package gui;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import metagrammar.Operator;
import util.OperatorWrapper;

/**
 * @author Joshua Lockerman
 *
 */
public class OperatorComboBox extends JComboBox<OperatorWrapper> {
    /**
     * 
     */
    private final OperatorModelProvider modelProvider;
    
    private class ModelUpdater implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            updateModel();
        }
    }
    
    OperatorComboBox(OperatorModelProvider modelProvider)
    {
        this.modelProvider = modelProvider;
        updateModel();
    }
    
    /**
     * @param libraryComboBox TODO
     * 
     */
    OperatorComboBox(LibraryComboBox libraryComboBox)
    {
        this((OperatorModelProvider) libraryComboBox);
        libraryComboBox.addActionListener(new ModelUpdater());
    }
    
    /**
     * 
     */
    private void updateModel()
    {
        setModel(modelProvider.getOperatorModel());
        if(getItemCount() > 0) {
            setSelectedIndex(0);
        }
    }
    
    public Operator getSelectedOperator()
    {
        return getItemAt(getSelectedIndex()).getOperator();
    }
}